package com.hxzy.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.AjaxResult;
import com.hxzy.common.core.domain.R;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.utils.poi.ExcelUtil;
import com.hxzy.system.domain.SysPost;
import com.hxzy.system.domain.dto.SysPostDTO;
import com.hxzy.system.service.ISysPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping(value = "api")
@Api(tags = "岗位管理接口")
public class SysPostController extends BaseController {


    @Autowired
    private ISysPostService iSysPostService;

    @GetMapping(value = "/system/post/list")
    @ApiOperation(value = "分页查询")
    public R selectPostList (SysPostDTO sysPostDTO) {
        IPage<SysPost> page = this.iSysPostService.selectPostList(sysPostDTO);
        return pageVO(page);
    }


    @Transactional
    @PostMapping(value = "/system/add")
    @ApiOperation(value = "新增岗位")
    public AjaxResult addPost (@RequestBody @Validated SysPost sysPost) {
        if (this.iSysPostService.checkPostNameUnique(sysPost.getPostName())) {
            return AjaxResult.error("新增失败,已存在该名称");
        }
        if (this.iSysPostService.checkPostCodeUnique(sysPost.getPostCode())) {
            return AjaxResult.error("新增失败,已存在该编码");
        }

        boolean save = this.iSysPostService.save(sysPost);
        return save ? AjaxResult.success() : AjaxResult.error();
    }


    @GetMapping(value = "/system/post/{id}")
    @ApiOperation(value = "根据id查询信息")
    @ApiImplicitParam(name = "id", value = "主键")
    public R selectPostById (@PathVariable(value = "id") Long id) {
        SysPost post = this.iSysPostService.getById(id);
        return Objects.nonNull(post) ? R.okHasData(post) : R.build(AckCode.NOT_FOUND_DATA);
    }


    @ApiOperation(value = "根据id修改岗位信息")
    @PutMapping(value = "/system/edit")
    public AjaxResult edit (@RequestBody @Validated SysPost sysPost) {
//        if (this.iSysPostService.checkPostNameUnique(sysPost.getPostName())) {
//            return AjaxResult.error("修改失败,已存在该名称");
//        }
//        if (this.iSysPostService.checkPostCodeUnique(sysPost.getPostCode())) {
//            return AjaxResult.error("修改失败,已存在该编码");
//        }
        boolean b = this.iSysPostService.updateById(sysPost);
        return b ? AjaxResult.success() : AjaxResult.error();
    }

    @DeleteMapping(value = "/system/post/{id}")
    @ApiOperation(value = "根据id删除信息")
    @ApiImplicitParam(name = "id", value = "主键")
    public AjaxResult remove (@PathVariable Long id) {
        boolean bo = this.iSysPostService.selectPostAndUserById(id);
        if (bo) {
            return AjaxResult.error("该岗位已分配不能删除");
        }
//        boolean b = this.iSysPostService.removeById(id);
        SysPost sysPost = this.iSysPostService.getById(id);
        sysPost.setStatus("1");
        boolean b = this.iSysPostService.updateById(sysPost);
        return b ? AjaxResult.success("成功") : AjaxResult.error();
    }

    @PostMapping(value = "/system/post/export")
    @ApiOperation(value = "查询导出数据")
    public void exportExcel (HttpServletResponse response, SysPostDTO sysPostDTO) {
        List<SysPost> list = this.iSysPostService.searchExport(sysPostDTO);
        ExcelUtil<SysPost> util = new ExcelUtil<>(SysPost.class);
        util.exportExcel(response,list, "岗位数据");
    }
}
